﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using blackhouse.Data;
using blackhouse.Payments.Data.Sql;

namespace blackhouse.Payments.Data {
    public class SqlPaymentsDataAccess : AdoPaymentsDataAccess {

        #region Fields

        private MsSqlAdoDataHelper dataHelper;

        #endregion

        #region Constructors

        public SqlPaymentsDataAccess() : this(GetConnectionString()) { }

        public SqlPaymentsDataAccess(string connectionString) {
            this.dataHelper = new MsSqlAdoDataHelper(connectionString);
            SqlVersioning versioningDb = new SqlVersioning(connectionString, typeof(SqlPaymentsDataFactory).Assembly);
        }

        #endregion

        #region Methods

        protected override IDataReader GetPaymentTypeReader(string paymentKey) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_PayU_PaymentTypeGet";
            this.dataHelper.AddParameter("PaymentKey", paymentKey);
            return this.dataHelper.Execute();
        }

        protected override IDataReader GetPaymentTransactionStatusReader(int statusId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_PayU_PaymentStatusGet";
            this.dataHelper.AddParameter("StatusId", statusId, DbType.Int32);
            return this.dataHelper.Execute();
        }

        private static string GetConnectionString() {
            return ConfigurationManager.AppSettings["MSSQLConnectionString"];
        }

        public override int SaveNewPaymentTransaction(PaymentTransactionData data) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_PayU_PaymentTransactionSaveNew";
            this.dataHelper.AddParameter("PosId", data.PosId, DbType.Int32);
            this.dataHelper.AddParameter("PosAuthKey", data.PosAuthKey, DbType.String);
            this.dataHelper.AddParameter("PaymentType", data.PaymentType, DbType.String);
            this.dataHelper.AddParameter("ChargeValue", data.ChargeValue, DbType.Double);
            this.dataHelper.AddParameter("Description", data.Description, DbType.String);
            this.dataHelper.AddParameter("FirstName", data.FirstName, DbType.String);
            this.dataHelper.AddParameter("LastName", data.LastName, DbType.String);
            this.dataHelper.AddParameter("SessionId", data.UserSessionId, DbType.String);
            this.dataHelper.AddParameter("EMail", data.EMail, DbType.String);
            this.dataHelper.AddParameter("ClientIP", data.ClientIP, DbType.String);
            this.dataHelper.AddParameter("Status", data.Status, DbType.Int32);
            this.dataHelper.AddParameter("OrderID", data.OrderID, DbType.Int32);
            this.dataHelper.AddParameter("Description2", data.Description2, "", DbType.String);
            this.dataHelper.AddParameter("DescriptionBank", data.DescriptionBank, "", DbType.String);
            this.dataHelper.AddParameter("Street", data.Street, "", DbType.String);
            this.dataHelper.AddParameter("StreetNumber", data.StreetNumber, "", DbType.String);
            this.dataHelper.AddParameter("FlatNumber", data.FlatNumber, "", DbType.String);
            this.dataHelper.AddParameter("City", data.City, "", DbType.String);
            this.dataHelper.AddParameter("PostCode", data.PostCode, "", DbType.String);
            this.dataHelper.AddParameter("Country", data.Country, DbType.String);
            this.dataHelper.AddParameter("Phone", data.Phone, "", DbType.String);
            this.dataHelper.AddParameter("Language", data.Language, "", DbType.StringFixedLength);
            this.dataHelper.AddParameter("JavaScript", data.JavaScript, false, DbType.Boolean);
            this.dataHelper.AddParameter("PaybackLogin", data.PaybackLogin, "", DbType.String);
            this.dataHelper.AddParameter("Sig", data.Sig, DbType.String);
            this.dataHelper.AddParameter("Ts", data.Ts, DbType.String);
            return Convert.ToInt32(this.dataHelper.ExecuteScalar());
        }

        protected override IDataReader FindPaymentTransactionReader(int posId, string transactionSession) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_PayU_PaymentTransactionFind";
            this.dataHelper.AddParameter("PosId", posId, DbType.Int32);
            this.dataHelper.AddParameter("SessionId", transactionSession);
            return this.dataHelper.Execute();
        }

        protected override IDataReader GetPaymentErrorReader(int errorId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_PayU_PaymentErrorGet";
            this.dataHelper.AddParameter("ErrorId", errorId, DbType.Int32);
            return this.dataHelper.Execute();
        }

        public override void AddPaymentTransactionErrorLog(int transactionId, int errorId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_PayU_PaymentTransactionLogError";
            this.dataHelper.AddParameter("TransactionId", transactionId, DbType.Int32);
            this.dataHelper.AddParameter("ErrorId", errorId, DbType.Int32);
            this.dataHelper.ExecuteWithoutResult();
        }

        public override void AddPaymentTransactionPositiveLog(int transactionId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_PayU_PaymentTransactionLogPositive";
            this.dataHelper.AddParameter("TransactionId", transactionId, DbType.Int32);
            this.dataHelper.ExecuteWithoutResult();
        }

        public override int AddPaymentTransactionLog(int transactionId, PaymentTransactionData data) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_PayU_PaymentTransactionLog";
            this.dataHelper.AddParameter("TransactionId", transactionId, DbType.Int32);
            this.dataHelper.AddParameter("PosId", data.PosId, DbType.Int32);
            this.dataHelper.AddParameter("PosAuthKey", data.PosAuthKey, DbType.String);
            this.dataHelper.AddParameter("PaymentType", data.PaymentType, DbType.String);
            this.dataHelper.AddParameter("ChargeValue", data.ChargeValue, DbType.Double);
            this.dataHelper.AddParameter("Description", data.Description, DbType.String);
            this.dataHelper.AddParameter("FirstName", data.FirstName, DbType.String);
            this.dataHelper.AddParameter("LastName", data.LastName, DbType.String);
            this.dataHelper.AddParameter("SessionId", data.UserSessionId, DbType.String);
            this.dataHelper.AddParameter("EMail", data.EMail, DbType.String);
            this.dataHelper.AddParameter("ClientIP", data.ClientIP, DbType.String);
            this.dataHelper.AddParameter("Status", data.Status, DbType.Int32);
            this.dataHelper.AddParameter("OrderID", data.OrderID, DbType.Int32);
            this.dataHelper.AddParameter("Description2", data.Description2, DbType.String);
            this.dataHelper.AddParameter("DescriptionBank", data.DescriptionBank, DbType.String);
            this.dataHelper.AddParameter("Street", data.Street, DbType.String);
            this.dataHelper.AddParameter("StreetNumber", data.StreetNumber, DbType.String);
            this.dataHelper.AddParameter("FlatNumber", data.FlatNumber, DbType.String);
            this.dataHelper.AddParameter("City", data.City, DbType.String);
            this.dataHelper.AddParameter("PostCode", data.PostCode, DbType.String);
            this.dataHelper.AddParameter("Country", data.Country, DbType.String);
            this.dataHelper.AddParameter("Phone", data.Phone, DbType.String);
            this.dataHelper.AddParameter("Language", data.Language, DbType.StringFixedLength);
            this.dataHelper.AddParameter("JavaScript", data.JavaScript, DbType.Boolean);
            this.dataHelper.AddParameter("PaybackLogin", data.PaybackLogin, DbType.String);
            this.dataHelper.AddParameter("Sig", data.Sig, DbType.String);
            this.dataHelper.AddParameter("Ts", data.Ts, DbType.String);
            if(data.CreatedDate!=DateTime.MinValue)
            this.dataHelper.AddParameter("CreatedDate", data.CreatedDate, DbType.DateTime);
            if (data.BeginDate != DateTime.MinValue)
                this.dataHelper.AddParameter("BeginDate", data.BeginDate, DbType.DateTime);
            if (data.SentDate != DateTime.MinValue)
                this.dataHelper.AddParameter("SentDate", data.SentDate, DbType.DateTime);
            if (data.ReceiveDate != DateTime.MinValue)
                this.dataHelper.AddParameter("ReceiveDate", data.ReceiveDate, DbType.DateTime);
            if (data.CancelDate != DateTime.MinValue)
                this.dataHelper.AddParameter("CancelDate", data.CancelDate, DbType.DateTime);
            return Convert.ToInt32(this.dataHelper.ExecuteScalar());
        }

        public override void SavePaymentTransaction(int transactionId, PaymentTransactionData data) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_PayU_PaymentTransactionSave";
            this.dataHelper.AddParameter("TransactionId", transactionId, DbType.Int32);
            this.dataHelper.AddParameter("PosId", data.PosId, DbType.Int32);
            this.dataHelper.AddParameter("PosAuthKey", data.PosAuthKey, DbType.String);
            this.dataHelper.AddParameter("PaymentType", data.PaymentType, DbType.String);
            this.dataHelper.AddParameter("ChargeValue", data.ChargeValue, DbType.Double);
            this.dataHelper.AddParameter("Description", data.Description, DbType.String);
            this.dataHelper.AddParameter("FirstName", data.FirstName, DbType.String);
            this.dataHelper.AddParameter("LastName", data.LastName, DbType.String);
            this.dataHelper.AddParameter("SessionId", data.UserSessionId, DbType.String);
            this.dataHelper.AddParameter("EMail", data.EMail, DbType.String);
            this.dataHelper.AddParameter("ClientIP", data.ClientIP, DbType.String);
            this.dataHelper.AddParameter("Status", data.Status, DbType.Int32);
            this.dataHelper.AddParameter("OrderID", data.OrderID, DbType.Int32);
            this.dataHelper.AddParameter("Description2", data.Description2, DbType.String);
            this.dataHelper.AddParameter("DescriptionBank", data.DescriptionBank, DbType.String);
            this.dataHelper.AddParameter("Street", data.Street, DbType.String);
            this.dataHelper.AddParameter("StreetNumber", data.StreetNumber, DbType.String);
            this.dataHelper.AddParameter("FlatNumber", data.FlatNumber, DbType.String);
            this.dataHelper.AddParameter("City", data.City, DbType.String);
            this.dataHelper.AddParameter("PostCode", data.PostCode, DbType.String);
            this.dataHelper.AddParameter("Country", data.Country, DbType.String);
            this.dataHelper.AddParameter("Phone", data.Phone, DbType.String);
            this.dataHelper.AddParameter("Language", data.Language, DbType.StringFixedLength);
            this.dataHelper.AddParameter("JavaScript", data.JavaScript, DbType.Boolean);
            this.dataHelper.AddParameter("PaybackLogin", data.PaybackLogin, DbType.String);
            this.dataHelper.AddParameter("Sig", data.Sig, DbType.String);
            this.dataHelper.AddParameter("Ts", data.Ts, DbType.String);
            if (data.CreatedDate != DateTime.MinValue)
                this.dataHelper.AddParameter("CreatedDate", data.CreatedDate, DbType.DateTime);
            if (data.BeginDate != DateTime.MinValue)
                this.dataHelper.AddParameter("BeginDate", data.BeginDate, DbType.DateTime);
            if (data.SentDate != DateTime.MinValue)
                this.dataHelper.AddParameter("SentDate", data.SentDate, DbType.DateTime);
            if (data.ReceiveDate != DateTime.MinValue)
                this.dataHelper.AddParameter("ReceiveDate", data.ReceiveDate, DbType.DateTime);
            if (data.CancelDate != DateTime.MinValue)
                this.dataHelper.AddParameter("CancelDate", data.CancelDate, DbType.DateTime);
            this.dataHelper.ExecuteWithoutResult();
        }

        protected override IDataReader GetPaymentTransactionReader(int paymentTransactionId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_PayU_PaymentTransactionGet";
            this.dataHelper.AddParameter("TransactionId", paymentTransactionId, DbType.Int32);
            return this.dataHelper.Execute();
        }

        public override void SavePaymentTransactionUpdateSnapshot(int paymentTransactionId, string snapshot) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_PayU_PaymentTransactionSnapshotAdd";
            this.dataHelper.AddParameter("TransactionId", paymentTransactionId, DbType.Int32);
            this.dataHelper.AddParameter("Snapshot", snapshot, DbType.String);
            this.dataHelper.ExecuteWithoutResult();
        }

        protected override IDataReader GetPaymentTransactionByOrderIdReader(int orderId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_PayU_PaymentTransactionGetByOrderId";
            this.dataHelper.AddParameter("OrderId", orderId, DbType.Int32);
            return this.dataHelper.Execute();
        }

        #endregion

    }
}